package com.xinxing.learning.datasource.context;

import java.util.ArrayDeque;
import java.util.Deque;

public class DynamicContextHolder {
    private static ThreadLocal<Deque<String>> CONTEXT_HOLDER = new ThreadLocal() {
        @Override
        protected Object initialValue() {
            return new ArrayDeque<String>();
        }
    };

    public static String peek() {
        return CONTEXT_HOLDER.get().peek();
    }

    public static void push(String dbKey) {
        CONTEXT_HOLDER.get().push(dbKey);
    }

    public static void poll() {
        Deque<String> deque = CONTEXT_HOLDER.get();
        deque.clear();
        if (deque.isEmpty())
            CONTEXT_HOLDER.remove();
    }
}
